/*
 * @Author: huangrongji
 * @Date: 2021-01-30 21:43:33
 * LastEditors: 黄荣基
 * LastEditTime: 2021-03-11 15:28:09
 * @Description: file content
 * @FilePath: \news_testd:\工作空间\workspace\repair\pages\user\Login\login.js
 */
// pages/user/login/login.js
const app = getApp();
const $api = require('../../../service/api.js').API;
Page({
	/**
   * 页面的初始数据
   */
	data: {
		imgUrl: app.globalData.imgUrl
	},

	goBack() {
		wx.navigateBack({
			delta: 1 //返回上一个页面
		});
	},

	newLogin(e) {
		const that = this;
		wx.login({
			success(res) {
				if (res.code) {
					console.log(res.code);
					//发起网络请求
					const data = {
						code: res.code,
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv
					};
					$api
						.getOpenid(data)
						.then((res) => {
							//请求成功
							console.log(res);
							wx.hideLoading();
							if (res.code == 11 || res.flag) {
								wx.setStorageSync('openid', res.data.openid);
								wx.setStorageSync('token', res.data.token);
								wx.setStorageSync('phoneNum', res.data.wxPhone.phoneNumber);
								wx.setStorageSync('userInfo', res.data.user_info);
								wx.showToast({
									title: '登录成功',
									icon: 'success',
									duration: 2000
								});
								setTimeout(() => that.goBack(), 2000);
							} else if (res.code == 100 && !res.flag) {
								wx.showToast({
									title: '未绑定手机号',
									icon: 'none',
									duration: 2000
								});
								setTimeout(() => that.goBack(), 2000);
							} else {
								console.log('登录失败！' + res.message);
								wx.showToast({
									title: '登录失败！请重新登录',
									icon: 'none',
									duration: 2000
								});
								setTimeout(() => that.goBack(), 2000);
							}
						})
						.catch((err) => {
							//请求失败
							console.log(err);
						});
				} else {
					console.log('登录失败！' + res.message);
					wx.showToast({
						title: '登录失败！请重新登录',
						icon: 'none',
						duration: 2000
					});
					setTimeout(() => that.goBack(), 2000);
				}
			}
		});
	},

	login(e) {
		wx.checkSession({
			success: () => {
				let that = this;
				const openid = wx.getStorageSync('openid');
				if (openid) {
					const data = {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						openid: openid
					};
					$api
						.getPhone(data)
						.then((res) => {
							if (res.code == 11) {
								console.log(res);
								wx.showToast({
									title: '获取手机号成功',
									icon: 'success',
									duration: 2000
								});
								wx.setStorageSync('phoneNum', res.data.phoneNumber);
								setTimeout(() => that.goBack(), 2000);
							} else {
								console.log('登录失败！' + res.message);
								wx.showToast({
									title: '登录失败！请重新登录',
									icon: 'none',
									duration: 2000
								});
								setTimeout(() => that.goBack(), 2000);
							}
						})
						.catch((err) => {
							//请求失败
							console.log(err);
						});
				} else {
					this.newLogin(e);
				}
			},
			fail: () => {
				this.newLogin(e);
			}
		});
	},

	/**
   * 生命周期函数--监听页面加载
   */
	onLoad: function(options) {},

	/**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function() {},

	/**
   * 生命周期函数--监听页面显示
   */
	onShow: function() {},

	/**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function() {},

	/**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function() {},

	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function() {},

	/**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function() {},

	/**
   * 用户点击右上角分享
   */
	onShareAppMessage: function() {}
});
